package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/5/18
 */
@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {
    @Autowired
    StatMapper statMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Override
    public List<CategorySellAmts> findCategorySellData() {
        return statMapper.findCategorySellData();
    }

    @Override
    public List<CatVo> findCategoryList() {
        List<CatVo> catVos = new ArrayList<>();
        LambdaQueryWrapper<DtsCategory> qw= new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getPid, 0);
        qw.eq(DtsCategory::getDeleted, false);
        qw.select(DtsCategory::getId,DtsCategory::getName);
        List<DtsCategory> dtsCategories = categoryMapper.selectList(qw);
        for (DtsCategory dtsCategory : dtsCategories) {
            CatVo catVo = new CatVo();
            catVo.setValue(dtsCategory.getId());
            catVo.setLabel(dtsCategory.getName());

            LambdaQueryWrapper<DtsCategory> qw2 = new LambdaQueryWrapper<>();
            qw2.select(DtsCategory::getId,DtsCategory::getName);
            qw2.eq(DtsCategory::getPid, dtsCategory.getId());
            qw2.eq(DtsCategory::getDeleted, false);
            List<CatVo> catVos1 = new ArrayList<>();
            List<DtsCategory> children = categoryMapper.selectList(qw2);
            for (DtsCategory child : children) {
                CatVo catVo1 = new CatVo();
                catVo1.setValue(child.getId());
                catVo1.setLabel(child.getName());
                catVos1.add(catVo1);
            }
            catVo.setChildren(catVos1);
            catVos.add(catVo);

        }


        return catVos;
    }
}
